package com.example.demo.Controller;

import com.alibaba.fastjson2.JSON;
import com.example.common.redis.RedisCache;
import com.example.common.utils.AjaxResult;
import com.example.common.utils.TrimSpaceUtil;
import com.example.demo.domain.DTO.*;
import com.example.demo.domain.*;
import com.example.demo.service.SpotInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 景点Controller
 */
@RestController
//@RequestMapping("/spot")
@RequestMapping("/api/other/spot")

@EnableAsync
public class SpotController {
    @Autowired
    private RedisCache redisCache;

    @Autowired
    private SpotInfoService spotInfoService;

    /**
     * 查询全部
     * @return
     */
    @GetMapping("/all")
    public AjaxResult spotAll(){
        try {
            //查询redis是否存在数据
            List<SpotInfo> redisInfo = redisCache.getCacheObject("spotAll");
            //如果缓存中已存在则直接返回
            return AjaxResult.success("查询成功",redisInfo);

        }catch (NullPointerException e) {
            List<SpotInfo> spotInfos = spotInfoService.selectAll();
            //缓存时间30分钟
            redisCache.setCacheJSONObject("spotAll", spotInfos, 1440, TimeUnit.MINUTES);
            return AjaxResult.success("查询成功", spotInfos);
        }
    }

    /**
     * 热度前十的景点
     *
     * @return
     */
    @GetMapping("/hotTop10")
    public AjaxResult spotHotTop10() {
        try {
            //查询redis是否存在数据
            List<SpotHotInfo> redisInfo = redisCache.getCacheObject("spotHotTop5");
            return AjaxResult.success("查询成功", redisInfo);
        } catch (NullPointerException e) {
            //热度前十的景点
            List<SpotHotInfo> spotHotInfos = spotInfoService.selectHotInfoTop10();
            //存入redis
            redisCache.setCacheJSONObject("spotHotTop5", spotHotInfos, 1440, TimeUnit.MINUTES);

            return AjaxResult.success("查询成功", spotHotInfos);
        }
    }

    /**
     * 推荐指数前十的景点
     *
     * @return
     */
    @GetMapping("/ratingTop10")
    public AjaxResult ratingTop10() {
        try {
            //查询redis是否存在数据
            List<SpotHotInfo> redisInfo = redisCache.getCacheObject("ratingTop10");
            return AjaxResult.success("查询成功", redisInfo);
        } catch (NullPointerException e) {
            List<SpotInfo> spotInfos = spotInfoService.selectRatingTop10();
            //设置缓存30分钟
            redisCache.setCacheJSONObject("ratingTop10", spotInfos, 1440, TimeUnit.MINUTES);
            return AjaxResult.success("查询成功", spotInfos);
        }
    }


    /**
     * 星评前十的景点
     *
     * @return
     */
    @GetMapping("/gradeTop10")
    public AjaxResult spotGradeTop10() {
        try {
            //查询redis是否存在数据
            List<SpotGradeInfo> redisInfo = redisCache.getCacheObject("spotGradeTop5");
            return AjaxResult.success("查询成功", redisInfo);
        } catch (NullPointerException e) {
            List<SpotGradeInfo> spotGradeInfos = spotInfoService.selectGradeTop10();
            //设置缓存30分钟
            redisCache.setCacheJSONObject("spotGradeTop5", JSON.toJSONString(spotGradeInfos), 1440, TimeUnit.MINUTES);
            return AjaxResult.success("查询成功", spotGradeInfos);
        }
    }

    /**
     * 返回一个指定景区的信息
     *
     * @param spotId 景区ID
     * @return
     */
    @GetMapping("/spot/item/{spotid}")
    public AjaxResult spotOneInfo(@PathVariable("spotid") int spotId) {
        try {
            //查询redis是否存在数据
            return AjaxResult.success("查询成功", redisCache.getCacheObject("spotOneInfo_" + spotId));
        } catch (NullPointerException e) {
            SpotInfo spotInfo = spotInfoService.selectSpotInfoById(spotId);
            //指定查找一个景区可能传进来的ID是没有指定景区信息的所有需要判断
            if (spotInfo == null) {
                return AjaxResult.error("无spotId为:" + spotId + "的景区信息");
            }
            //去换行
            SpotInfo result = (SpotInfo) TrimSpaceUtil.replaceBlankSpace(spotInfo);

            //设置缓存30分钟
            redisCache.setCacheJSONObject("spotOneInfo_" + spotId, result, 30, TimeUnit.MINUTES);
            return AjaxResult.success("查询成功", spotInfo);
        }
    }


    /**
     * 点评最多(按照景区表的SUM)
     *
     * @return
     */
    @GetMapping("/spotBySum")
    public AjaxResult spotBySum() {
        try {
            //查询redis是否存在数据
            return AjaxResult.success("查询成功", redisCache.getCacheObject("maxSumSpot"));
        } catch (NullPointerException e) {
            SpotInfo spotGradeInfos = (SpotInfo) TrimSpaceUtil.replaceBlankSpace(spotInfoService.selectMaxSum());
            //设置缓存30分钟
            redisCache.setCacheJSONObject("maxSumSpot", spotGradeInfos, 1440, TimeUnit.MINUTES);
            return AjaxResult.success("查询成功", spotGradeInfos);
        }
    }

    /**
     * 季节最热门景区(x轴 季节 y轴 景区  z轴 该季度总评论数)
     *
     * @return
     */
    @GetMapping("/seasonSpotSumInfo")
    public AjaxResult seasonSpotSumInfo() {
        try {
            //查询redis是否存在数据
            return AjaxResult.success("查询成功", redisCache.getCacheObject("seasonSpot"));
        } catch (NullPointerException e) {
            HashMap<String, HashMap<String, Object>> seasonSpotInfo = spotInfoService.selectSeasonSpotInfo();
            //设置缓存30分钟
            redisCache.setCacheJSONObject("seasonSpot", seasonSpotInfo, 1440, TimeUnit.MINUTES);
            return AjaxResult.success("查询成功", seasonSpotInfo);
        }
    }

    /**
     * 季节前十评论数的景区
     *
     * @return
     */
    @GetMapping("/seasonSpot10SumInfo")
    public AjaxResult seasonSpot10SumInfo() {
        try {
            //查询redis是否存在数据
            return AjaxResult.success("查询成功", redisCache.getCacheObject("spot10Info"));
        } catch (NullPointerException e) {
            HashMap<String, List<SeasonSpot10Info>> spot10Info = spotInfoService.selectSeasonSpot10Info();
            //设置缓存30分钟
            redisCache.setCacheJSONObject("spot10Info", spot10Info, 1440, TimeUnit.MINUTES);
            return AjaxResult.success("查询成功", spot10Info);
        }
    }


    /**
     * 景区搜索栏
     *
     * @return 返回18个景区的ID name address provinceName
     */
    @GetMapping("/spotInfoSearch")
    public AjaxResult SpotInfoSearch() {
        try {
            //查询redis是否存在数据
            return AjaxResult.success("查询成功", redisCache.getCacheObject("spotInfoSearch"));
        } catch (NullPointerException e) {
            List<SpotSearch> spotSearches = spotInfoService.selectSpotOtherInfo();
            //设置缓存30分钟
            redisCache.setCacheJSONObject("spotInfoSearch", spotSearches, 1440, TimeUnit.MINUTES);
            return AjaxResult.success("查询成功", spotSearches);
        }
    }


    /**
     * 各景区评论数
     *
     * @return 返回18个景区的ID name address provinceName
     */
    @GetMapping("/spotComInfo")
    public AjaxResult SpotComInfo() {
        try {
            //查询redis是否存在数据
            return AjaxResult.success("查询成功", redisCache.getCacheObject("spotComInfo"));
        } catch (NullPointerException e) {
            List<SpotComCntInfo> spotSearches = spotInfoService.selectSpotComCntInfo();
            //设置缓存30分钟
            redisCache.setCacheJSONObject("spotComInfo", spotSearches, 1440, TimeUnit.MINUTES);
            return AjaxResult.success("查询成功", spotSearches);
        }
    }

    /**
     * 单个景点的各项指标
     *
     * @param spotId 景点ID
     * @return 指标信息
     */
    @GetMapping("/spotFiveInfo/{spotId}")
    public AjaxResult SpotFiveInfo(@PathVariable("spotId") int spotId) {
        try {
            //查询redis是否存在数据
            return AjaxResult.success("查询成功", redisCache.getCacheObject("spotFiveInfo_" + spotId));
        } catch (NullPointerException e) {
            SpotFiveInfo spotFiveInfo = spotInfoService.selectSpotFiveInfo(spotId);
            if (spotFiveInfo == null) {
                return AjaxResult.error("无spotId为:" + spotId + "的景区信息");
            }
            //设置缓存30分钟
            redisCache.setCacheJSONObject("spotFiveInfo_" + spotId, spotFiveInfo, 30, TimeUnit.MINUTES);
            return AjaxResult.success("查询成功", spotFiveInfo);
        }
    }


    /**
     * 全部景点的平均各项指标
     *
     * @return 指标信息
     */
    @GetMapping("/spotAllFiveInfo")
    public AjaxResult spotAllFiveInfo() {
        try {
            //查询redis是否存在数据
            return AjaxResult.success("查询成功", redisCache.getCacheObject("spotAllFiveInfo"));
        } catch (NullPointerException e) {
            SpotAvgFiveInfo spotAvgFiveInfo = spotInfoService.selectAllSpotAvgFiveInfo();
            //设置缓存30分钟
            redisCache.setCacheJSONObject("spotAllFiveInfo", spotAvgFiveInfo, 1440, TimeUnit.MINUTES);
            return AjaxResult.success("查询成功", spotAvgFiveInfo);
        }
    }

    /**
     * rating前十景区的各月评论数
     */
    @GetMapping("/ratingTop10CntInfo")
    public AjaxResult ratingTop10CntInfo() {
        try {
            //查询redis是否存在数据
            return AjaxResult.success("查询成功", redisCache.getCacheObject("ratingTop10CntInfo"));
        } catch (NullPointerException e) {
            HashMap<String, HashMap<String, Object>> result = spotInfoService.selectRatingTop10CntInfo();
            //设置缓存30分钟
            redisCache.setCacheJSONObject("ratingTop10CntInfo", result, 1440, TimeUnit.MINUTES);
            return AjaxResult.success("查询成功", result);
        }
    }

    /**
     * rating前十景区的各省的人数
     */
    @GetMapping("/ratingTop10ProvinceInfo")
    public AjaxResult ratingTop10ProvinceInfo() {
        try {
            //查询redis是否存在数据
            return AjaxResult.success("查询成功", redisCache.getCacheObject("RatingTop10ProvinceInfo"));
        } catch (NullPointerException e) {
            HashMap<String, HashMap<String, Object>> result = spotInfoService.selectRatingTop10ProvinceInfo();
            //设置缓存30分钟
            redisCache.setCacheJSONObject("selectRatingTop10ProvinceInfo", result, 1440, TimeUnit.MINUTES);
            return AjaxResult.success("查询成功", result);
        }
    }

    /**
     * 各月景点评论和平均口碑
     *
     * @return 各月景点评论和平均口碑
     */
    @GetMapping("/stringListHashMap")
    public AjaxResult spotMonthInfo() {
        try {
            //查询redis是否存在数据
            return AjaxResult.success("查询成功", redisCache.getCacheObject("spotMonthInfoList"));
        } catch (NullPointerException e) {
            HashMap<String, List<SpotMonthInfo>> spotMonthInfoListHashMap = spotInfoService.selectSpotMonthInfo();
            //设置缓存30分钟
            redisCache.setCacheJSONObject("spotMonthInfoList", spotMonthInfoListHashMap, 30, TimeUnit.MINUTES);
            return AjaxResult.success("查询成功", spotMonthInfoListHashMap);
        }
    }


    /**
     * 某个景区来自各个省的游客数
     */
    @GetMapping("/spotProvincePeopleNum/{spotId}")
    public AjaxResult spotProvincePeopleNum(@PathVariable("spotId") int spotId) {
        try {
            //查询redis是否存在数据
            return AjaxResult.success("查询成功", redisCache.getCacheObject("spotProvinceNum_" + spotId));
        } catch (NullPointerException e) {
            if (spotInfoService.selectSpotInfoById(spotId) == null) {
                return AjaxResult.error("没有该景区信息");
            }
            SpotProvinceDto spotProvinceDto = spotInfoService.selectSpotProvincePeopleNum(spotId);
            //设置缓存30分钟
            redisCache.setCacheJSONObject("spotProvinceNum_" + spotId, spotProvinceDto, 30, TimeUnit.MINUTES);
            return AjaxResult.success("查询成功", spotProvinceDto);
        }
    }


    /**
     * 景区等级数量
     */
    @GetMapping("/SpotLevelCnt")
    public AjaxResult SpotLevelCnt() {
        try {
            //查询redis是否存在数据
            return AjaxResult.success("查询成功", redisCache.getCacheObject("SpotLevelCnt"));
        } catch (NullPointerException e) {
            List<SpotLevelCnt> spotLevelCnts = spotInfoService.selectSpotLevelCnt();
            //设置缓存一天
            redisCache.setCacheJSONObject("SpotLevelCnt", spotLevelCnts, 1440, TimeUnit.MINUTES);
            return AjaxResult.success("查询成功", spotLevelCnts);
        }
    }

    /**
     * 接口测试
     *
     * @return
     */
    @GetMapping("/test")
    public AjaxResult testT() {
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("湖北省武汉市武昌区西山坡特1号\\n            \\n                \\n            \\n            ");
        Object userReplace = TrimSpaceUtil.replaceBlankSpace(userInfo);
        return AjaxResult.success("测试", userReplace);
//        return null;
    }
}
